package tkaczyk.simtalk.android;

import tkaczyk.simtalk.message.Message;
import tkaczyk.simtalk.messenger.MessageDisplay;
import tkaczyk.simtalk.messenger.StatusDisplay;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ChatService extends Service implements MessageDisplay, StatusDisplay {
	private static String TAG = "std.ChatService";
	private SimTalkDroid app;
	
	public static final String STATUS_UPDATE = "tkaczyk.simtalk.messenger.STATUS_UPDATE";
	public static final String CHAT_UPDATE = "tkaczyk.simtalk.messenger.CHAT_UPDATE";
//	public static final String SEND_MESSAGE = "tkaczyk.simtalk.messenger.SEND_MESSAGE";
//	public static final String CONNECT = "tkaczyk.simtalk.messenger.CONNECT";
//	public static final String LISTEN = "tkaczyk.simtalk.messenger.LISTEN";
//	public static final String DISCONNECT = "tkaczyk.simtalk.messenger.DISCONNECT";
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.i(TAG, "Starting service");
		app = (SimTalkDroid) super.getApplication();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "onStartCommand");
		super.onStartCommand(intent, flags, startId);
		
		app.getMessenger().registerMessageDisplay(this);
		app.getMessenger().registerStatusDisplay(this);
		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
	@Override
	public void update(Status status, String statusMessage) {
		app.updateStatus(status, statusMessage);
		Intent intent = new Intent(STATUS_UPDATE);
		sendBroadcast(intent, STATUS_UPDATE);	
	}

	@Override
	public void update(Message msg) {
		app.receiveMessage(msg);
		Intent intent = new Intent(CHAT_UPDATE);
		sendBroadcast(intent, CHAT_UPDATE);	
	}
}
